// pages/user/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeNames: ['1'],
    volunteerStatus:false,
    user:{},
    avatar:'/static/images/basicprofile.jpeg'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const that = this
    that.api_getUserInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onShareTimeline(){

  },
  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  volunteerApply(e) {
    const that = this

    if(that.data.user.volunteer) {
      wx.navigateTo({
        url: '/pages/volunteer/state/index',
      })
      return ;
    }
    this.api_decryptUserInfo(e.detail.iv, e.detail.encryptedData);
    return ;
    wx.navigateTo({
      url: '/pages/volunteer/apply/index',
    })
  },
  myActivity() {
    wx.navigateTo({
      url: '/pages/volunteer/activity/index',
    })
  },
  myRecord(){
    wx.navigateTo({
      url: '/pages/volunteer/record/index',
    })
  },//获取当前用户信息
  api_getUserInfo(){
    const that = this
    app.api.get(app.getUrl('volunteer.info')).then((res)=>{
      that.setData({
        user:res
      })
      if(res.volunteer && res.volunteer.status == 2) {
        that.setData({
          volunteerStatus:true,
        })
      }
    })
  },
  // 更新头像 昵称信息
  api_decryptUserInfo(iv, encrypted_data) {
    const data = {
      app_id: app.app_id,
      iv: iv,
      encrypted_data: encrypted_data
    };
  
    app.api.post(app.getUrl('decrypt.user.info'),data).then((res)=>{
      console.log('res', res)
      wx.navigateTo({
        url: '/pages/volunteer/apply/index',
      })
    })
    
    
  }
})